package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.Company;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.RecordCourse;
import com.xmy.cultivate.entity.StudentGrade;
import com.xmy.cultivate.entity.respense.StudentStatus;
import com.xmy.cultivate.mapper.OrderDetailMapper;
import com.xmy.cultivate.mapper.StudentGradeMapper;
import com.xmy.cultivate.service.ICompanyService;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.IStudentGradeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 学生所在班级与课程（分班表） 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2023-08-17
 */
@Service
public class StudentGradeServiceImpl extends ServiceImpl<StudentGradeMapper, StudentGrade> implements IStudentGradeService {

    @Autowired
    StudentGradeMapper studentGradeMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    ICompanyService iCompanyService;

    @Override
    public StudentStatus getStudentStatus(Long studentId, Integer lessonType){

        Quarter quarter = iQuarterService.getNowQuarter();
        Long subjectsId =1L;

        Map<String,String> map = new HashMap<>();

        Integer rs = 1;//1正常，2是不在读，3是剩余课时为0
        String msg = "正常";
        StudentStatus studentStatus = new StudentStatus();
        Company company = iCompanyService.getById(1);
        if (company.getEnAbleLogin().equals(1)){
            studentStatus.setStatus(rs);
            studentStatus.setMessage(msg);
            return studentStatus;
        }else if (company.getEnAbleLogin().equals(2)){
            QueryWrapper<StudentGrade> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("year_part", quarter.getYearPart());
            queryWrapper.eq("quarter_num", quarter.getNum());
            queryWrapper.eq("student_id", studentId);
            queryWrapper.eq("subjects_id", subjectsId);
            queryWrapper.eq("lesson_type", lessonType);
            queryWrapper.ne("status", 3);
            //queryWrapper.in("")
            queryWrapper.last("limit 1");
            StudentGrade studentGrade = studentGradeMapper.selectOne(queryWrapper);
            if (studentGrade != null){
                if (studentGrade.getYearClassId()>9){
                    //高中能登陆
                    studentStatus.setStatus(rs);
                    studentStatus.setMessage(msg);
                    return studentStatus;
                }
            }
        }

        //暂时调整为了在读和停课的都能登陆
        Integer count =  studentGradeMapper.getStudentStatus(quarter.getYearPart(),quarter.getNum(),subjectsId,studentId,lessonType);


        if (count.equals(0)){
            rs = 2;
            msg = "没有在读状态，不能登陆";
        }

        Integer yearAndQuarter = Integer.parseInt(quarter.getYearPart()+""+quarter.getNum());
        //Integer useCourseCount = studentGradeMapper.getUseCourseCount(quarter.getYearPart(),quarter.getNum(),1L,lessonType,studentId);
        /*if (courseHouse<=0){
            Integer useCourseCount = studentGradeMapper.getUseCourseCount(quarter.getYearPart(),quarter.getNum(),1L,lessonType,studentId);
            Company company = iCompanyService.getById(1);
            if (company.getEnAbleLogin().equals(1)){
                if (useCourseCount>0){
                    rs = 1;
                    msg = "登陆成功(剩余课时为0，当前为“赠送课时”,请正常学习)";
                }else {
                    rs = 3;
                    msg = "剩余课时为0，不能登陆";
                }
            }else {
                //使用课时小于15课次
                if (useCourseCount < 45){
                    rs = 3;
                    msg = "剩余课时为0，不能登陆";
                }else {
                    rs = 1;
                    msg = "登陆成功(剩余课时为0，当前为“赠送课时”,请正常学习)";
                }
            }
        }*/


        if (company.getEnAbleLogin().equals(1)){
            Integer allResidueHouse = orderDetailMapper.getAllResidueHouse(null,studentId,1L,yearAndQuarter,lessonType);
            Integer useCourseCount = studentGradeMapper.getUseCourseCount(quarter.getYearPart(),quarter.getNum(),1L,lessonType,studentId);
            if (useCourseCount>0){
                rs = 1;
                msg = "登陆成功(剩余课时为0，当前为“赠送课时”,请正常学习)";
            }else if (allResidueHouse>0){
                rs = 1;
                msg = "正常";
            }else {
                rs = 3;
                msg = "剩余课时为0，不能登陆";
            }
        }
        studentStatus.setStatus(rs);
        studentStatus.setMessage(msg);
        return studentStatus;
    }

}
